<script setup lang="tsx">
import { Form, FormSchema } from '@/components/Form'
import { PropType, reactive, ref } from 'vue'
import { useValidator } from '@/hooks/web/useValidator'
import { SysImportantApiLogDetailVo } from '@/api/system/log/SysImportantApiLog/types'
import { isBlank } from '@/utils/StringUtils'
import { useBaseWriteView } from '@/base/views/BaseWriteView'
import { sysDictChooseListApi } from '@/api/system/dict/SysDict'
import { KeyLabelValueVo } from '@/base/api/KeyLabelValueVo'
// [[ OnflowGenerator Range (- selectFunctionImport -) Start ]]
import { selectSysAccountNameListApi } from '@/api/system/account/SysAccount'
import { selectSysApiNameListApi } from '@/api/system/api/SysApi'
// [[ OnflowGenerator Range (- selectFunctionImport -) End ]]

const { required } = useValidator()

const props = defineProps({
  currentRow: {
    type: Object as PropType<Nullable<SysImportantApiLogDetailVo>>,
    default: () => null
  },
  editType: {
    type: String,
    default: 'add'
  },
  editId: {
    type: Number,
    default: -1
  }
})
const isAdd = ref(props.editType === 'add')

const { formRegister, submit, doWatch } = useBaseWriteView(props)
doWatch()

const formRef = ref<typeof Form>()
// [[ OnflowGenerator Range (- dictFunctions -) Start ]]
const platformTypeOptions = ref<Array<KeyLabelValueVo>>([])
const getPlatformType = async () => {
  const res = await sysDictChooseListApi('platformType')
  platformTypeOptions.value = res.data
}
getPlatformType()

// [[ OnflowGenerator Range (- dictFunctions -) End ]]

// [[ OnflowGenerator Range (- selectFunctions -) Start ]]
const sysAccountNameList = ref<Array<KeyLabelValueVo>>([])
const getSysAccountNameList = async () => {
  const res = await selectSysAccountNameListApi({})
  sysAccountNameList.value = res.data
}
getSysAccountNameList()
const sysApiNameList = ref<Array<KeyLabelValueVo>>([])
const getSysApiNameList = async () => {
  const res = await selectSysApiNameListApi({})
  sysApiNameList.value = res.data
}
getSysApiNameList()

// [[ OnflowGenerator Range (- selectFunctions -) End ]]

const schema = reactive<FormSchema[]>([
  // [[ OnflowGenerator Range (- inputSchema -) Start ]]
  {
    field: 'accountId',
    label: '账号',
    component: 'Select',
    componentProps: {
      placeholder: '请选择账号id',
      clearable: true,
      filterable: true,
      options: sysAccountNameList
    }
  },
  {
    field: 'requestTime',
    label: '请求时间',
    component: 'DatePicker',
    componentProps: {
      placeholder: '请选择请求时间',
      clearable: true,
      type: 'datetime',
      valueFormat: 'YYYY-MM-DD HH:mm:ss'
    }
  },
  {
    field: 'responseTime',
    label: '响应时间',
    component: 'DatePicker',
    componentProps: {
      placeholder: '请选择响应时间',
      clearable: true,
      type: 'datetime',
      valueFormat: 'YYYY-MM-DD HH:mm:ss'
    }
  },
  {
    field: 'handleTime',
    label: '处理时间',
    component: 'InputNumber',
    componentProps: {
      placeholder: '请选择处理时间',
      clearable: true
    }
  },
  {
    field: 'instanceId',
    label: '实例id',
    value: '1',
    component: 'Input',
    componentProps: {
      placeholder: '请输入实例id',
      clearable: true
    }
  },
  {
    field: 'url',
    label: '接口地址',
    component: 'Input',
    componentProps: {
      placeholder: '请输入接口地址',
      clearable: true
    }
  },
  {
    field: 'apiId',
    label: '接口',
    component: 'Select',
    componentProps: {
      placeholder: '请选择接口id',
      clearable: true,
      filterable: true,
      options: sysApiNameList
    }
  },
  {
    field: 'returnCode',
    label: '返回码',
    component: 'Input',
    componentProps: {
      placeholder: '请输入返回码',
      clearable: true
    }
  },
  {
    field: 'sourceIp',
    label: '来源ip',
    component: 'Input',
    componentProps: {
      placeholder: '请输入来源ip',
      clearable: true
    }
  },
  {
    field: 'platformType',
    label: '平台类型',
    value: 'onflow-backend',
    component: 'Select',
    componentProps: {
      placeholder: '请选择平台类型',
      clearable: true,
      filterable: true,
      options: platformTypeOptions
    }
  },
  {
    field: 'failReason',
    label: '失败原因',
    component: 'Input',
    componentProps: {
      placeholder: '不存在失败原因',
      clearable: true
    }
  },
  {
    field: 'requestBody',
    label: '请求参数',
    component: 'Input',
    componentProps: {
      placeholder: '请输入请求参数',
      clearable: true,
      type: 'textarea',
      rows: 10
    },
    colProps: {
      span: 24
    }
  },
  {
    field: 'responseBody',
    label: '返回参数',
    component: 'Input',
    componentProps: {
      placeholder: '请输入返回参数',
      clearable: true,
      type: 'textarea',
      rows: 10
    },
    colProps: {
      span: 24
    }
  },
  // [[ OnflowGenerator Range (- inputSchema -) End ]]
  {
    field: 'createTime',
    label: '创建时间',
    component: 'Input',
    remove: isAdd.value,
    componentProps: {
      placeholder: '暂无创建时间',
      disabled: true
    }
  },
  {
    field: 'updateTime',
    label: '更新时间',
    component: 'Input',
    remove: isAdd.value,
    componentProps: {
      placeholder: '暂无更新时间',
      disabled: true
    }
  }
])

const rules = reactive({
  // [[ OnflowGenerator Range (- rules -) Start ]]
  requestTime: [required()],
  responseTime: [required()],
  handleTime: [required()],
  instanceId: [
    required(),
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 100) {
          callback(new Error('实例id不能超过100个字符'))
        } else {
          callback()
        }
      }
    }
  ],
  url: [
    required(),
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 500) {
          callback(new Error('接口地址不能超过500个字符'))
        } else {
          callback()
        }
      }
    }
  ],
  apiId: [required()],
  returnCode: [required()],
  sourceIp: [
    required(),
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 64) {
          callback(new Error('来源ip不能超过64个字符'))
        } else {
          callback()
        }
      }
    }
  ],
  platformType: [
    required(),
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 100) {
          callback(new Error('平台类型不能超过100个字符'))
        } else {
          callback()
        }
      }
    }
  ],
  failReason: [
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 5000) {
          callback(new Error('失败原因不能超过5000个字符'))
        } else {
          callback()
        }
      }
    }
  ],
  requestBody: [
    required(),
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 10000) {
          callback(new Error('请求参数不能超过10000个字符'))
        } else {
          callback()
        }
      }
    }
  ],
  responseBody: [
    required(),
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 10000) {
          callback(new Error('返回参数不能超过10000个字符'))
        } else {
          callback()
        }
      }
    }
  ]
  // [[ OnflowGenerator Range (- rules -) End ]]
})

// 所有内容全部都是不可编辑
schema.forEach((item) => {
  item.componentProps.disabled = true
})

defineExpose({
  submit
})
</script>

<template>
  <Form :rules="rules" @register="formRegister" :schema="schema" ref="formRef" />
</template>
